<?php 

Class appModel extends Model
{

	public function get_app()
	{
		$arr=array();
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appid' OR op_key='appsecret'";
		$temp=$this->getAll($sql);
		$arr['appid']=$temp[0]['op_value'];
		$arr['appsecret']=$temp[1]['op_value'];
		return $arr;
	}
	public function toggle($cfg)
	{
		$sql='SELECT '.$cfg['name']." FROM ".$this->table($cfg['table'])." WHERE id='".$cfg['id']."'";
		$res=$this->getOne($sql);
		
		if($res>=0&&$res<$cfg['status_num']-1)
		{
			$set=$cfg['name']."=".$cfg['name']."+1";
		}
		elseif($res>=$cfg['status_num']-1) 
		{
			$set=$cfg['name']."=0";
		}
		$sql="UPDATE ".$this->table($cfg['table'])." SET  ".$set." WHERE id='".$cfg['id']."'";
		$resut=$this->query($sql);
		return $result;
	}
	//保存配置
	public function saveCfg($key,$val)
	{
		$sql="SELECT 1 FROM ".$this->table('options')." WHERE op_key='".$key."'";
		$res=$this->getOne($sql);
	
		if($res)
		{
			$sql="UPDATE ".$this->table('options')." SET op_value='".$val."' WHERE op_key='".$key."'";
			$result=$this->query($sql);
		}
		else 
		{
			$sql="INSERT ".$this->table('options')."(op_key,op_value) VALUE ('".$key."','".$val."')";
			$result=$this->query($sql);
		}
		return $result;
	}
	
	/**
	 * 获取权限的ID
	 * @param unknown $action
	 */
	public function  get_action_id($action)
	{
		$sql="SELECT mod_id FROM ".$this->table('admin_action')." WHERE mod_code='".$action."'";
		return $this->getOne($sql);
	}
	
	public function get_cat_id($id,$table)
	{
		$sql="SELECT cat_id FROM ".$this->table($table)." WHERE id=".$id;
		return $this->getOne($sql);
	}
	public function check_admin()
	{
		
		if ((!isset($_SESSION['admin_id']) || intval($_SESSION['admin_id']) <= 0) &&
		ACTION_NAME!= 'login' )
		{
			/* session 不存在，检查cookie */
			if (!empty($_COOKIE['QIAQIA']['admin_id']) && !empty($_COOKIE['QIAQIA']['admin_pass']))
			{
				// 找到了cookie, 验证cookie信息
				$sql = 'SELECT id, username, password' .
						' FROM ' .$this->table('admin') .
						" WHERE id = '" . intval($_COOKIE['QIAQIA']['admin_id']) . "'";
				$row = $this->getRow($sql);
		
				if (!$row)
				{
					// 没有找到这个记录
					cookie($_COOKIE['QIAQIA']['admin_id'],   '');
					cookie($_COOKIE['QIAQIA']['admin_pass'], '');
		
					return false;
				}
				else
				{
					// 检查密码是否正确
					if (md5($row['password']) == $_COOKIE['QIAQIA']['admin_pass'])
					{
						
						
		
						session('admin_id',$row['user_id']);
						session('admin_name',$row['user_name']);
						return true;
					}
					else
					{
						cookie($_COOKIE['QIAQIA']['admin_id'],   '');
						cookie($_COOKIE['QIAQIA']['admin_pass'], '');
		
						return false;
					}
				}
			}
			else
			{
				return false;
			}
		}
		else 
		{
			return true;
		}
			
	}
	public function make_thumb($img, $thumb_width = 0, $thumb_height = 0, $path = '', $bgcolor='')
	{
		return Model('image')->make_thumb(ROOT_PATH.$img, $thumb_width, $thumb_height , $path , $bgcolor);
			
	}
	
	/**
	 * 获得权限名称
	 */
	public function get_action_name($id = '')
	{
		$data = array();
		if(!empty($id))
		{
			if(!is_array($id))
			{
				$data[] = $this->getOne("select action_list from ".$this->table('admin_action_list')." where action_id=".$id);
			}
			else 
			{
				foreach($id as $k => $v)
				{
					$data[] = $this->getOne("select action_list from ".$this->table('admin_action_list')." where action_id=".$v);
				}
			}
		}
		return $data;
	}
	public function get_cfg($key='')
	{
		if($key)
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='".$key."'";
			$arr=$this->getOne($sql);
				
		}
		else
		{
			$sql="SELECT * FROM ".$this->table('options')." ";
			$arr=array();
			$res=$this->getAll($sql);
			 
			if(count($res)>0)
			{
				foreach($res as $k=>$v)
				{
					$arr[$v['op_key']]=$v['op_value'];
				}
			}
		}
	
		return $arr;
	}
}